package com.didi;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
@RestController
public class RedisTest {

    public String key = "apiCount";
    public boolean flag = false;

    public static void main(String[] args) {
        SpringApplication.run(RedisTest.class, args);
    }

    // 使用redis模板，操作redis
    public RedisTemplate<Object, Object > redisTemplate;

    @Autowired(required = false)
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(stringSerializer);
        this.redisTemplate = redisTemplate;
    }

    @RequestMapping("/api")
    public void api() {
        if(this.flag) {
//            long count = redisTemplate.opsForValue().increment(key, 1);
//            if (count == 1) {
//                //设置有效期一分钟
//                redisTemplate.expire(key, 60, TimeUnit.SECONDS);
//            }

//            redisTemplate.opsForValue().increment(key, 1);

            long time = System.currentTimeMillis();
            redisTemplate.opsForZSet().add(key, "url" + time, time);
        }
    }

    @RequestMapping("/show")
    @ResponseBody
    public Long show() throws Exception {
//        String str = (String) this.redisTemplate.opsForValue().get(key);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = new GregorianCalendar();
        Date enddate = new Date();
        System.out.println("系统当前时间："+df.format(enddate));
        c.setTime(enddate);//设置参数时间
        c.add(Calendar.SECOND,-60);//把日期往后增加SECOND 秒.整数往后推,负数往前移动
        Date startdate=c.getTime(); //这个时间就是日期往后推一天的结果
        String str = df.format(startdate);
        System.out.println("系统前60秒时间："+str);

        Long count = this.redisTemplate.opsForZSet().count(key, startdate.getTime(), enddate.getTime());
        return count;
    }

    @RequestMapping("/start")
    public String start() throws Exception {
        this.flag = true;
        return "start";
    }

    @RequestMapping("/stop")
    public String stop() throws Exception {
        this.flag = false;
        return "stop";
    }

    @RequestMapping("/reset")
    public String reset() throws Exception {
//        redisTemplate.opsForValue().getAndSet(key, "0");
//        this.redisTemplate.opsForZSet().remove(key, "url*");

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = new GregorianCalendar();
        Date enddate = new Date();
        System.out.println("系统当前时间："+df.format(enddate));
        this.redisTemplate.opsForZSet().removeRangeByScore(key, 0, enddate.getTime());
        return "reset";
    }

	//第二个开发人员修改了当前文件

}
